<?php

class Action extends admin_abs
{


	function index(){
		$prent_id = _GET('prent_id', 0);
		if($prent_id){

			$data = _model('buildings')->getList("where prent_id = $prent_id order by id", new Pager(20));
			Smarty3::instance()->assign('prent_id', $prent_id);
			Smarty3::instance()->assign($data);
			Smarty3::instance()->display('building_list.html');
		}else{
			
			$data = _model('buildings')->getList("where prent_id = 0 order by id", new Pager(20));
			Smarty3::instance()->assign($data);
			Smarty3::instance()->display('build_list.html');
		}
		
		
	}


	function create(){

		$prent_id = _POST('prent_id', 0);
		$build_name = strip_tags(trim(_POST('name')));
		$index = strip_tags(strtoupper(trim(_POST('index_s'))));
		$info = _model('buildings')->read(array('name'=>$build_name));

		if ($_SERVER['REQUEST_METHOD'] === 'POST'){
			
			if(!$prent_id){
				if($info){
		
					msg('该区域已存在!');
				}

				if(!empty($build_name)){
	
					$cre = _model('buildings')->create(array('name'=>$build_name, 'prent_id'=>0));
					
					if($cre){
						msg('区域添加成功!', '?url=building');
					}
				}else{
				
					msg('区域不能为空!');
				}

			}else{
				
			
				if($info){
		
					msg('该楼宇已存在!');
				}

				if(!empty($build_name)){
	                if (empty($index)) {//quq 首字母自动处理
	                   $sub_str = substr_fix($build_name, 1);
	                   $sub_str = Pinyin($sub_str, 1);
	                   $index = strtoupper($sub_str[0]);
	                }
					$cre = _model('buildings')->create(array('name'=>$build_name, 'prent_id'=>$prent_id, 'index_s'=>$index));
					if($cre){
						msg('楼宇添加成功!',"?url=building&prent_id=$prent_id");
					}
				}else{
				
					msg('楼宇不能为空!');
				}
			}
			
		}else{
		
				$prent_id = _GET('prent_id', 0);
				Smarty3::instance()->assign('prent_id', $prent_id);
				Smarty3::instance()->display('build_create.html');
		}
	
	
	}


	function update(){

		if ($_SERVER['REQUEST_METHOD'] === 'POST'){

			$id = _POST('id', 0);
			$name = strip_tags(trim(_POST('name')));
			$index = strip_tags(strtoupper(trim(_POST('index_s'))));
				if (empty($index)) {//quq 首字母自动处理
			      
					$sub_str = substr_fix($name, 1);	
					$sub_str = Pinyin($sub_str, 1);
					$index = strtoupper($sub_str[0]);
	                	}
			$info = _model('buildings')->read(array('name'=>$name));

				if(empty($name)){
					msg('不能为空!');
				}
				

				if($info && $id!=$info['id']){
					if(!$info['prent_id']){
						msg('该区域已存在!');	
					}else{
						msg('该楼宇已存在!');
					}
				}
			
				if($id){
						
					$old = _model('buildings')->read(array('id'=>$id));
					
					if(!$old['prent_id']){
						$info = _model('buildings')->update(array('id'=>$id),array('name'=>$name));
							msg('区域修改成功!', "?url=building");
					}else{
					
							$info = _model('buildings')->update(array('id'=>$id),array('name'=>$name,'index_s'=>$index));
							msg('楼宇修改成功!', "?url=building&prent_id={$old['prent_id']}");	
					}			
				}
				
		}else{
			$id = _GET('id', 0);
			$info = _model('buildings')->read(array('id'=>$id));
		
			if($info){
				Smarty3::instance()->assign('info', $info);
				Smarty3::instance()->display('build_update.html');

			}else{
				msg('非法连接!');
			}
		}
		

	
	
	}


	function delete(){
		
	  $ids = _POST('id',array());

	  if ($_SERVER['REQUEST_METHOD'] === 'POST'){

		  $check = _model('buildings')->read(array('id'=>$ids['0']));

		  	if(!$check['prent_id']){

				  foreach($ids as $val){

					$info = _model('buildings')->getList("where prent_id=$val");
	 
				  	if($info){

						  foreach($info as $value){
				  
						  	 _model('buildings')->delete(array('id'=>$value['id']));
				  	
						  }
			  
		  				_model('buildings')->delete(array('id'=>$val));
					  }else{
		  
		  				_model('buildings')->delete(array('id'=>$val));
			
					  }	  
				}

		  		msg('区域删除成功', "?url=building");
		  	}else{

			  	foreach($ids as $value){
			  
			  		_model('buildings')->delete(array('id'=>$value));
			  	}
			  
			 	 msg('楼宇删除成功!',"?url=building&prent_id= {$check['prent_id']}");
		  	}
	  }else{
		  $id = _GET('id');
		  
		if($id){
			$info = _model('buildings')->read(array('id'=>$id));
			if($info){

				 if(!$info['prent_id']){
				
					 $data = _model('buildings')->getList("where prent_id = $id");
					 if($data){
						 foreach($data as $val){

						 	_model('buildings')->delete(array('id'=>$val['id']));
						 }
					
						 $del = _model('buildings')->delete(array('id'=>$id));
						 
					
					 	 if($del){
		 
							 msg('区域删除成功!',"?url=building");
				 
						 }else{
	
	 						 msg('区域错误!');	
			 
						 }
					 }else{

						 	$del = _model('buildings')->delete(array('id'=>$id));
							if($del){
							
								msg('区域删除成功!',"?url=building");
							}else{
							
								msg('区域错误!');	
							}
					 
					 }		

					 
				 }else{
			 
					 $del = _model('buildings')->delete(array('id'=>$id));
					 if($del){
						msg('楼宇删除成功!',"?url=building&prent_id= {$info['prent_id']}");
					 
					 }else{
				 		msg('非法链接!');
					 
					 }
				}
		
			}else{
				msg('非法链接!');
			}

		}else{
			msg('非法链接!');
		}
	
		 
	  }
	}	



}

?>
